package chapter04

import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.SparkSession

object Test02_Student {
  def main(args: Array[String]): Unit = {
    Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("student")
      .getOrCreate()
    //读取json
    val df = spark.read.json("input/score.json")
    //做成临时表
    df.createOrReplaceTempView("student")
    //计算每个班多少人
    spark.sql("select grade,count(*) num from student group by grade").show
    //计算总平均成绩
    spark.sql("select avg(score) as avg_score from student").show
    //计算每个班的平均成绩
    spark.sql(
      """select grade,
        |avg(score) as avg_score
        |from student
        |group by grade""".stripMargin).show
    //第二种方式 DSL
    df.groupBy("grade").count().show
    df.groupBy().avg("score").show
    df.groupBy("grade").avg("score").show
    //关闭
    spark.close()
  }
}
